package zhang.lao.service.console;/**
 * Created by zhangqin on 2017/8/30.
 */

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import zhang.lao.build.kit.ehcache.CacheKit;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.redis.RedisUtil;
import zhang.lao.build.tool.MapTool;
import zhang.lao.build.tool.NumberUtils;
import zhang.lao.build.tool.date.DateStyle;
import zhang.lao.build.tool.date.DateUtil;
import zhang.lao.dao.base.*;
import zhang.lao.pojo.console.ConsoleCacheNameContanst;
import zhang.lao.pojo.console.map.MapSerieResp;
import zhang.lao.pojo.console.report.AreaReportResp;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @create 2017-08-30 13:30
 **/

@Service
public class ReportService {
    @Autowired
    private WnMachineDeviceActiveGt06Dao wnMachineDeviceActiveGt06Dao;
    @Autowired
    private WnMachineWorkReportAutoDao wnMachineWorkReportAutoDao;

    @Autowired
    private WnMachineDao wnMachineDao;

    @Autowired
    private WnMachineDeviceDao wnMachineDeviceDao;

    @Autowired
    private WnMachineToolDao wnMachineToolDao;

    @Autowired
    private WnFarmlandDao wnFarmlandDao;

    @Autowired
    private WnFarmDao wnFarmDao;
    private static final List<String> colorChoose = Lists.newArrayList("#99CCCC","#336633","#CC9966","#0099CC","#CCCCFF","#CCFFFF","#FFCC99","#FF6666","#FFFFCC","#9933FF","#663300","#CC6699","#339966","#663333","#CCFFFF","#FF99CC","#FFCC99","#CAFF70","#CAE1FF","#C9C9C9","#C7C7C7","#C71585","#C6E2FF","#C67171","#C5C1AA","#C4C4C4","#C2C2C2","#C1FFC1","#C1CDCD","#C1CDC1","#C1C1C1","#C0FF3E","#BFEFFF","#BFBFBF","#BF3EFF","#BEBEBE","#BDBDBD","#BDB76B","#BCEE68","#BCD2EE","#BC8F8F","#BBFFFF","#BABABA","#BA55D3","#B9D3EE","#B8B8B8","#B8860B","#B7B7B7","#B5B5B5","#B4EEB4","#B4CDCD","#B452CD","#B3EE3A","#B3B3B3","#B2DFEE","#B23AEE","#B22222","#B0E2FF","#B0E0E6","#B0C4DE","#B0B0B0","#B03060","#AEEEEE","#ADFF2F","#ADD8E6","#ADADAD","#ABABAB","#AB82FF","#AAAAAA","#A9A9A9","#A8A8A8","#A6A6A6","#A52A2A","#A4D3EE","#A3A3A3","#A2CD5A","#A2B5CD","#A1A1A1","#A0522D","#A020F0","#9FB6CD","#9F79EE","#9E9E9E","#9C9C9C","#9BCD9B","#9B30FF","#9AFF9A","#9ACD32","#9AC0CD","#9A32CD","#999999","#9932CC","#98FB98","#98F5FF","#97FFFF","#96CDCD","#969696","#949494","#9400D3","#9370DB","#919191","#912CEE","#90EE90","#8FBC8F","#8F8F8F","#8EE5EE","#8E8E8E","#8E8E38","#8E388E","#8DEEEE","#8DB6CD","#8C8C8C","#8B8B83","#8B8B7A","#8B8B00","#8B8989","#8B8970","#8B8878","#8B8682","#8B864E","#8B8386","#8B8378","#8B814C","#8B7E66","#8B7D7B","#8B7D6B","#8B7B8B","#8B795E");

    /**
     * 获取面积auto
     */
    public String getMachineArea(String date,List<WnMachineDevice> wnMachineDeviceList){
        double areaSize =0.00;
        for (WnMachineDevice device : wnMachineDeviceList) {
            Integer deviceId = device.getId();
            WnMachineWorkReportAutoExample wnLnglatAutomaticExample = new WnMachineWorkReportAutoExample();
            wnLnglatAutomaticExample.createCriteria().andDeviceIdEqualTo(deviceId).andWorkDateEqualTo(date);
            List<WnMachineWorkReportAuto> wnLnglatAutomatics = wnMachineWorkReportAutoDao.selectByExample(wnLnglatAutomaticExample);
            for (WnMachineWorkReportAuto wnLnglatAutomatic : wnLnglatAutomatics) {
                areaSize+=wnLnglatAutomatic.getWorkArea();
            }
        }

        return NumberUtils.keepPrecision(new BigDecimal(areaSize), 2).toString();
    }


    @Autowired
    private WnMachineWorkReportGt06Dao wnMachineWorkReportGt06Dao;

    /**
     * 获取面积gto6
     */
    public String getMachineAreaGt06(String date,List<WnMachineDevice> wnMachineDeviceList){
        double areaSize =0.00;
        for (WnMachineDevice device : wnMachineDeviceList) {
            Integer deviceId = device.getId();
            WnMachineWorkReportGt06Example wnLnglatAutomaticExample = new WnMachineWorkReportGt06Example();
            wnLnglatAutomaticExample.createCriteria().andDeviceIdEqualTo(deviceId).andWorkDateEqualTo(date);
            List<WnMachineWorkReportGt06> wnLnglatAutomatics = wnMachineWorkReportGt06Dao.selectByExample(wnLnglatAutomaticExample);
            for (WnMachineWorkReportGt06 wnLnglatAutomatic : wnLnglatAutomatics) {
                areaSize+=wnLnglatAutomatic.getWorkArea();
            }
        }

        return NumberUtils.keepPrecision(new BigDecimal(areaSize), 2).toString();
    }
    public AreaReportResp getActiveRespGt06(String startDate, String endDate, List<String> farms) {
        List<String> beforeDay = new ArrayList<>();
        if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
            beforeDay = DateUtil.getBeforeDay(30, new Date(), DateStyle.YYYY_MM_DD);
        } else if (StringUtils.isBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            beforeDay = DateUtil.getBeforeDay(30, DateUtil.StringToDate(endDate), DateStyle.YYYY_MM_DD);
        } else if (StringUtils.isNotBlank(startDate) && StringUtils.isBlank(endDate)) {
            beforeDay = DateUtil.getBeetwenDate(startDate, DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD));
        } else {
            beforeDay = DateUtil.getBeetwenDate(startDate, endDate);
        }

        List<String> names = new ArrayList<>();
        List<String> colors = new ArrayList<>();
        List<MapSerieResp> mapSerieResps = new ArrayList<>();
        for (int i = 0; i < farms.size(); i++) {
            String farm = farms.get(i);
            colors.add(colorChoose.get(i));
            MapSerieResp mapSerieResp = new MapSerieResp();
            List<String> v = new ArrayList<>();
            for (String s : beforeDay) {
                WnMachineDeviceActiveGt06Example wnMachineDeviceActiveExample = new WnMachineDeviceActiveGt06Example();
                wnMachineDeviceActiveExample.createCriteria().andFarmIdEqualTo(farm).andCreateTimeGreaterThan(DateUtil.StringToDate(s + " 00:00:00")).andCreateTimeLessThan(DateUtil.StringToDate(s + " 23:59:59"));
                int i1 = wnMachineDeviceActiveGt06Dao.countByExample(wnMachineDeviceActiveExample);
                v.add(i1 + "");
            }
            mapSerieResp.setData(v);
            WnFarm wnFarm = wnFarmDao.selectByPrimaryKey(farm);
            mapSerieResp.setName(wnFarm.getName());
            names.add(wnFarm.getName());
            mapSerieResps.add(mapSerieResp);
        }
        AreaReportResp areaReportResp = new AreaReportResp(beforeDay, mapSerieResps, names);
        areaReportResp.setColors(colors);
        return areaReportResp ;
    }
    public AreaReportResp getAreaReportRespGt06(String startDate, String endDate, List<String> farms) {
        List<String> beforeDay = new ArrayList<>();
        if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
            beforeDay = DateUtil.getBeforeDay(30, new Date(), DateStyle.YYYY_MM_DD);
        } else if (StringUtils.isBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            beforeDay = DateUtil.getBeforeDay(30, DateUtil.StringToDate(endDate), DateStyle.YYYY_MM_DD);
        } else if (StringUtils.isNotBlank(startDate) && StringUtils.isBlank(endDate)) {
            beforeDay = DateUtil.getBeetwenDate(startDate, DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD));
        } else if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            beforeDay = DateUtil.getBeetwenDate(startDate, endDate);
        }

        List<String> names = new ArrayList<>();
        List<String> colors = new ArrayList<>();
        List<MapSerieResp> mapSerieResps = new ArrayList<>();
        for (int i = 0; i < farms.size(); i++) {
            String farm = farms.get(i);
            colors.add(colorChoose.get(i));
            MapSerieResp mapSerieResp = new MapSerieResp();
            List<String> v = new ArrayList<>();
            for (String s : beforeDay) {
                String s1 = RedisUtil.getInstance().strings().get(s + "" + farm + "gt06");
                if (StringUtils.isNotBlank(s1)) {
                    v.add(s1);
                } else {
                    WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
                    wnMachineDeviceExample.createCriteria().andFarmIdEqualTo(farm);
                    List<WnMachineDevice> wnMachineDeviceList = wnMachineDeviceDao.selectByExample(wnMachineDeviceExample);
                    String machineAreaGt06 = getMachineAreaGt06(s, wnMachineDeviceList);
                    v.add(machineAreaGt06);
                    RedisUtil.getInstance().strings().set(s + "" + farm + "gt06", machineAreaGt06);
                }
            }
            mapSerieResp.setData(v);
            WnFarm wnFarm = wnFarmDao.selectByPrimaryKey(farm);
            mapSerieResp.setName(wnFarm.getName());
            names.add(wnFarm.getName());
            mapSerieResps.add(mapSerieResp);
        }
        AreaReportResp areaReportResp = new AreaReportResp(beforeDay, mapSerieResps, names);
        areaReportResp.setColors(colors);
        return  areaReportResp ;
    }

    public AreaReportResp getAreaReportRespAuto(String startDate, String endDate, List<String> farms){

        List<String> beforeDay = new ArrayList<>() ;
        if(StringUtils.isBlank(startDate)&&StringUtils.isBlank(endDate)){
            beforeDay = DateUtil.getBeforeDay(30,new Date(), DateStyle.YYYY_MM_DD_CN);
        }else if(StringUtils.isBlank(startDate)&&StringUtils.isNotBlank(endDate)){
            beforeDay = DateUtil.getBeforeDay(30,DateUtil.StringToDate(endDate),DateStyle.YYYY_MM_DD_CN);
        }else if(StringUtils.isNotBlank(startDate)&&StringUtils.isBlank(endDate)){
            beforeDay = DateUtil.getBeetwenDate(startDate,DateUtil.DateToString(new Date(),DateStyle.YYYY_MM_DD));
        }else if(StringUtils.isNotBlank(startDate)&&StringUtils.isNotBlank(endDate)){
            beforeDay = DateUtil.getBeetwenDate(startDate,endDate);
        }

        List<String> names = new ArrayList<>();
        List<String> colors = new ArrayList<>();
        List<MapSerieResp> mapSerieResps = new ArrayList<>();
        for (int i = 0; i < farms.size(); i++) {
            String farm =farms.get(i);
            colors.add(colorChoose.get(i));
            MapSerieResp mapSerieResp = new MapSerieResp();
            List<String> v = new ArrayList<>();
            for (String s : beforeDay) {
                Object o = CacheKit.get(ConsoleCacheNameContanst.areaCache, s + "" + farm+"auto_machine");
                if(o!=null){
                    v.add(o.toString());
                }else {
                    WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
                    wnMachineDeviceExample.createCriteria().andFarmIdEqualTo(farm);
                    List<WnMachineDevice> wnMachineDeviceList = wnMachineDeviceDao.selectByExample(wnMachineDeviceExample);
                    String machineArea = getMachineArea(s, wnMachineDeviceList);
                    v.add(machineArea);
                    CacheKit.put(ConsoleCacheNameContanst.areaCache, s + "" + farm+"auto_machine",machineArea);
                }
            }
            mapSerieResp.setData(v);
            WnFarm wnFarm = wnFarmDao.selectByPrimaryKey(farm);
            mapSerieResp.setName(wnFarm.getName());
            names.add(wnFarm.getName());
            mapSerieResps.add(mapSerieResp);
        }
        AreaReportResp areaReportResp = new AreaReportResp(beforeDay, mapSerieResps, names);
        areaReportResp.setColors(colors);
        return areaReportResp;
    }

    @Autowired
    private WnMachineDeviceActiveDao wnMachineDeviceActiveDao;
    public AreaReportResp getActiveRespAuto(String startDate, String endDate, List<String> farms) {

        List<String> beforeDay = new ArrayList<>();
        if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
            beforeDay = DateUtil.getBeforeDay(30, new Date(), DateStyle.YYYY_MM_DD_CN);
        } else if (StringUtils.isBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            beforeDay = DateUtil.getBeforeDay(30, DateUtil.StringToDate(endDate), DateStyle.YYYY_MM_DD_CN);
        } else if (StringUtils.isNotBlank(startDate) && StringUtils.isBlank(endDate)) {
            beforeDay = DateUtil.getBeetwenDate(startDate, DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD));
        } else {
            beforeDay = DateUtil.getBeetwenDate(startDate, endDate);
        }

        List<String> names = new ArrayList<>();
        List<String> colors = new ArrayList<>();
        List<MapSerieResp> mapSerieResps = new ArrayList<>();
        for (int i = 0; i < farms.size(); i++) {
            String farm = farms.get(i);
            colors.add(colorChoose.get(i));
            MapSerieResp mapSerieResp = new MapSerieResp();
            List<String> v = new ArrayList<>();
            for (String s : beforeDay) {
                WnMachineDeviceActiveExample wnMachineDeviceActiveExample = new WnMachineDeviceActiveExample();
                wnMachineDeviceActiveExample.createCriteria().andFarmIdEqualTo(farm).andCreateTimeGreaterThan(DateUtil.StringToDate(s + " 00:00:00")).andCreateTimeLessThan(DateUtil.StringToDate(s + " 23:59:59"));
                int i1 = wnMachineDeviceActiveDao.countByExample(wnMachineDeviceActiveExample);
                v.add(i1 + "");
            }
            mapSerieResp.setData(v);
            WnFarm wnFarm = wnFarmDao.selectByPrimaryKey(farm);
            mapSerieResp.setName(wnFarm.getName());
            names.add(wnFarm.getName());
            mapSerieResps.add(mapSerieResp);
        }
        AreaReportResp areaReportResp = new AreaReportResp(beforeDay, mapSerieResps, names);
        areaReportResp.setColors(colors);
        return areaReportResp;
    }


    public JSONObject resourceReport(List<String> farms){

        WnFarmExample wnFarmExample = new WnFarmExample();
        wnFarmExample.createCriteria().andIdIn(farms);
        List<WnFarm> wnFarms = wnFarmDao.selectByExample(wnFarmExample);
        List<String> farmNames = Lists.newArrayList();
        List<JSONObject> valueList = Lists.newArrayList();
        JSONObject jsonObject = new JSONObject();
        if(wnFarms!=null){
            for (WnFarm wnFarm : wnFarms) {
                farmNames.add(wnFarm.getName());
                JSONObject jsonObjectV = new JSONObject();
                jsonObjectV.put("name",wnFarm.getName());
                List<Integer> listDate = Lists.newArrayList();

                WnMachineExample wnMachineExample = new WnMachineExample();
                wnMachineExample.createCriteria().andFarmIdEqualTo(wnFarm.getId());
                int machineCount = wnMachineDao.countByExample(wnMachineExample);
                int machineCountMax=machineCount*2==0?100:machineCount*2;
                if(jsonObject.getInteger("machine")!=null) {
                    if(jsonObject.getInteger("machine")<machineCountMax){
                        jsonObject.put("machine", machineCountMax);
                    }
                }else{
                    jsonObject.put("machine", machineCountMax);
                }
                listDate.add(machineCount);




                WnFarmlandExample wnFarmlandExample = new WnFarmlandExample();
                wnFarmlandExample.createCriteria().andFarmIdEqualTo(wnFarm.getId());
                int ntCount = wnFarmlandDao.countByExample(wnFarmlandExample);
                int ntCountMax=ntCount*2==0?100:ntCount*2;
                if(jsonObject.getInteger("farmLand")!=null) {
                    if(jsonObject.getInteger("farmLand")<ntCountMax){
                        jsonObject.put("farmLand", ntCountMax);
                    }
                }else{
                    jsonObject.put("farmLand", ntCountMax);
                }
                listDate.add(ntCount);


                WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
                wnMachineDeviceExample.createCriteria().andFarmIdEqualTo(wnFarm.getId()).andDeviceTypeEqualTo("windows_auto_machine");
                int machineDeviceCount = wnMachineDeviceDao.countByExample(wnMachineDeviceExample);
                int machineDeviceCountMax=machineDeviceCount*2==0?100:machineDeviceCount*2;
                if(jsonObject.getInteger("autoMachine")!=null) {
                    if(jsonObject.getInteger("autoMachine")<machineDeviceCountMax){
                        jsonObject.put("autoMachine", machineDeviceCountMax);
                    }
                }else{
                    jsonObject.put("autoMachine", machineDeviceCountMax);
                }
                listDate.add(machineDeviceCount);

                WnMachineDeviceExample wnMachineDeviceGt06Example = new WnMachineDeviceExample();
                wnMachineDeviceGt06Example.createCriteria().andFarmIdEqualTo(wnFarm.getId()).andDeviceTypeEqualTo("gt06");
                int gt06Count = wnMachineDeviceDao.countByExample(wnMachineDeviceGt06Example);
                int gt06Max=gt06Count*2==0?100:gt06Count*2;
                if(jsonObject.getInteger("gt06")!=null) {
                    if(jsonObject.getInteger("gt06")<gt06Max){
                        jsonObject.put("gt06", gt06Max);
                    }
                }else{
                    jsonObject.put("gt06", gt06Max);
                }
                listDate.add(gt06Count);


                WnMachineToolExample wnMachineToolExample = new WnMachineToolExample();
                wnMachineToolExample.createCriteria().andFarmIdEqualTo(wnFarm.getId());
                int machineToolCount = wnMachineToolDao.countByExample(wnMachineToolExample);
                int machineToolCountMax=machineToolCount*2==0?100:machineToolCount*2;
                if(jsonObject.getInteger("machineTool")!=null) {
                    if(jsonObject.getInteger("machineTool")<machineToolCountMax){
                        jsonObject.put("machineTool", machineToolCountMax);
                    }
                }else{
                    jsonObject.put("machineTool", machineToolCountMax);
                }
                listDate.add(machineToolCount);

                jsonObjectV.put("value",listDate);
                valueList.add(jsonObjectV);
            }
        }

        jsonObject.put("farmNames",farmNames);
        jsonObject.put("valueList",valueList);
        return jsonObject ;
    }
}
